//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Input;
using OpenLS.UI;
using OpenLS.UI.Internals;

namespace OpenLS.Spreadsheet.UI
{
    class GridGrouping
    {
        internal static void EnableCommands(UIElement grid)
        {
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.Group, OnGroup));
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.Ungroup, OnUngroup));
        }
        static void OnGroup(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet worksheet = SpreadsheetElement.GetWorksheet(sender);
            FullRange range = GetFullRange(worksheet.Selection);
            if (range == null)
                return;
            if (range is FullRowRange)
            {
                var fullRowRange = (FullRowRange) range;
                Row nextRow = worksheet.Rows[fullRowRange.LastRow.Index + 1];
                worksheet.GroupRows(fullRowRange.FirstRow, nextRow);
            }
            else if (range is FullColumnRange)
            {
                var fullRowRange = (FullColumnRange) range;
                Column nextColumn = worksheet.Columns[fullRowRange.LastColumn.Index + 1];
                worksheet.GroupColumns(fullRowRange.FirstColumn, nextColumn);
            }
            else
            {
                throw new NotSupportedException();
            }

            e.Handled = true;
        }
        static void OnUngroup(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet worksheet = SpreadsheetElement.GetWorksheet(sender);
            FullRange range = GetFullRange(worksheet.Selection);
            if (range == null)
                return;
            if (range is FullRowRange)
            {
                var fullRowRange = (FullRowRange)range;
                Row nextRow = worksheet.Rows[fullRowRange.LastRow.Index + 1];
                worksheet.UngroupRows(fullRowRange.FirstRow, nextRow);
            }
            else if (range is FullColumnRange)
            {
                var fullRowRange = (FullColumnRange)range;
                Column nextColumn = worksheet.Columns[fullRowRange.LastColumn.Index + 1];
                worksheet.UngroupColumns(fullRowRange.FirstColumn, nextColumn);
            }
            else
            {
                throw new NotSupportedException();
            }
            e.Handled = true;
        }
        static FullRange GetFullRange (Range range)
        {
            if (range is FullRange)
                return (FullRange) range;
            FullRange result = null;
            var control = new RowOrColumnControl();
            var container = new OKCancelUserControl {Content = {Content = control}};
            WindowServices.ShowDialog(container, Internals.Resources.UIStringTable.Group, null, 
                delegate(object o)
                    {
                        if (control.Rows.IsChecked.GetValueOrDefault())
                            result =   range.ToFullRowRange();
                        if (control.Columns.IsChecked.GetValueOrDefault())
                            result =  range.ToFullColumnRange();
                    },  o => false);
            return result;
        }

    }
}
